package com.hsnn.medstgmini.pro.base.dao.Impl;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.base.std.model.HospClassify;
import com.hsnn.medstgmini.base.std.model.HospLevel;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.base.dao.BaseHospDao;
import com.hsnn.medstgmini.pro.base.model.form.HospForm;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.AreaUtil;
import com.hsnn.medstgmini.util.Pagination;

@Repository
public class BaseHospDaoImpl extends BaseHibernateDAO implements BaseHospDao {

	@Autowired
	private HibernateSqlUtil dao;
	/**
	 * @category 根据条件查询商品目录
	 * @author 单大伟
	 * @date 2015年6月15日
	 * @param @param baseDelrelation
	 * @param @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	@Override
	public Pagination getHospListByPage(Pagination page) {
		String compType = page.getConditions().get("compType");
		String compId = page.getConditions().get("compId");
		String projId = page.getConditions().get("projId");
		String hospName = page.getConditions().get("hospName");
        String hospCl = page.getConditions().get("hospCl");
        String hospCode = page.getConditions().get("hospCode");
        String areaId = page.getConditions().get("areaId");
		
		StringBuffer hql = new StringBuffer();
		StringBuffer hqlEnd = new StringBuffer();
		StringBuffer hqlCount = new StringBuffer();
		hqlCount.append("select count(*) ");
		
		if (compType.equals("1")) {// 中心 
			return getHospListByPageFromZX(page);
		}
		
		if (compType.equals("2")) {// 生产企业
			hqlEnd.append(" select shi.hosp_id hospId, ")
				 .append("        shi.hosp_code hospCode, ")
				 .append("        shi.hosp_name hospName, ")
				 .append("        shi.area_id, ")
				 .append("        da.ar_shortname arfullname, ")
				 .append("        shi.hosp_classify hospClassify, ")
				 .append("        dh.hosp_type hospType, ")
				 .append("        shi.hosp_level_id hospLevelId, ")
				 .append("        shi.org_code orgCode   ")
				 .append("    from ")
				 .append("        std_hospinfo shi  ")
				 .append("    left join ")
				 .append("        dic_area da  ")
				 .append("            on shi.area_id=da.area_id  ")
				 .append("    left join ")
				 .append("        dic_hosptype dh  ")
				 .append("            on shi.hosp_type_id=dh.hosp_type_id   ")
				 .append("    left join ( ")
				 .append("      select distinct(bpjh.hosp_id)  ")
				 .append("            from ")
				 .append("                bid_project bpj  ")
				 .append("            left join ")
				 .append("                bid_projhospital bpjh  ")
				 .append("                    on bpj.proj_id = bpjh.proj_id   ")
				 .append("            where ")
				 .append("                bpj.proj_id in  ( ")
				 .append("                    SELECT ")
				 .append("                        distinct(bp.proj_id)  ")
				 .append("                    FROM ")
				 .append("                        bid_project bp  ")
				 .append("                    LEFT JOIN ")
				 .append("                        bid_projdrugs bpd  ")
				 .append("                            ON bp.proj_id = bpd.proj_id   ")
				 .append("                    INNER JOIN ")
				 .append("                        std_druginfo sdi  ")
				 .append("                            ON bpd.drug_id = sdi.drug_id   ")
				 .append("                    where ")
				 .append("                        bp.status = 1  ")
				 .append("                        and bp.starttime <= SYSDATE()  ")
				 .append("                        and bp.endtime >= SYSDATE()   ")
				 .append("                        and bpd.status=1   ")
				 .append("                        and sdi.status=1  ")
		         .append("                        and sdi.prod_comp_id= :compId ")
				 .append("                ) ")
				 .append("    )d on d.hosp_id=shi.hosp_id ")
				 .append("    where ")
				 .append("        shi.status = 1   ");
//			hql.append( "select  shi.hosp_id hospId, shi.hosp_code hospCode, shi.hosp_name hospName, shi.area_id, da.ar_fullname arfullname, ")
//			   .append(" shi.hosp_classify hospClassify, dh.hosp_type hospType, shi.hosp_level_id hospLevelId, shi.org_code orgCode ");
//			hqlEnd.append(" from std_hospinfo shi left join dic_area da on shi.area_id=da.area_id left join dic_hosptype dh on shi.hosp_type_id=dh.hosp_type_id ");
//			
//			
//			
//			 hqlEnd.append(" where shi.status = 1 ");
//			
			if (StringUtils.isNotBlank(hospName)) {
				hqlEnd.append(" and shi.hosp_name like :hospName ");
				page.getConditions().put("hospName", "%"+hospName+"%");
			}
			
			if (StringUtils.isNotBlank(hospCl)) {
				hqlEnd.append(" and shi.hosp_classify = :hospCl ");
			}
			
			if (StringUtils.isNotBlank(hospCode)) {
				hqlEnd.append(" and shi.hosp_code = :hospCode ");
			}
			
			if (StringUtils.isNotBlank(areaId)) {
				hqlEnd.append(" and shi.area_id like :areaId ");
				page.getConditions().put("areaId", "%"+areaId+"%");
			}
			page.getConditions().remove("compType");
			return dao.getSqlPageMapList(hqlEnd.toString(), page.getConditions(), page);
			
//			hqlEnd.append("and shi.hosp_id in ")
//			   .append(" (select distinct(bpjh.hosp_id) from bid_project bpj left join bid_projhospital bpjh on bpj.proj_id = bpjh.proj_id ")
//			   .append(" where bpj.proj_id in ")
//			   .append(" (SELECT distinct(bp.proj_id) FROM bid_project bp LEFT JOIN bid_projdrugs bpd ON bp.proj_id = bpd.proj_id ")
//			   .append(" INNER JOIN std_druginfo sdi ON bpd.drug_id = sdi.drug_id ")
//			   .append(" where bp.status = 1 and bp.starttime <= SYSDATE() and bp.endtime >= SYSDATE() ");
//			   	
//			if (StringUtils.isNotBlank(projId)) {
//				hqlEnd.append(" and bp.proj_id = :PID ");
//			}
//			
//			hqlEnd.append(" and bpd.status=1 ")
//			   .append(" and sdi.status=1 and sdi.prod_comp_id= :compId")
//			   .append("))");
		}
		
		if (compType.equals("3")) {// 配送企业
			hql.append("select DISTINCT shi.hosp_id hospId, shi.hosp_code hospCode, shi.hosp_name hospName, shi.area_id, da.ar_fullname arfullname, ")
			   .append(" shi.hosp_classify hospClassify, dh.hosp_type hospType, shi.hosp_level_id hospLevelId, shi.org_code orgCode ");
		
			hqlEnd.append(" FROM bid_projcompany bpc,bid_project bp,bid_projhospital bph,std_hospinfo shi ")
			  .append(" left join dic_hosptype dh on shi.hosp_type_id=dh.hosp_type_id  ")
			  .append(" left join dic_area da on shi.area_id=da.area_id  ")
			  .append(" where bpc.proj_id = bp.proj_id and bp.proj_id =bph.proj_id and shi.hosp_id = bph.hosp_id ")
			  .append(" and bpc.status=1 ")
			  .append(" and bp.status=1 ")
			  .append(" and bph.status=1 ")
			  .append(" and (SYSDATE() between bp.starttime and bp.endtime) ");
			
			if (StringUtils.isNotBlank(compId)) {
				hqlEnd.append(" and bpc.comp_id=:compId ");
			}
			if (StringUtils.isNotBlank(hospName)) {
				hqlEnd.append(" and shi.hosp_name like :HN ");
			}
			
			if (StringUtils.isNotBlank(hospCl)) {
				hqlEnd.append(" and shi.hosp_classify = :HCL ");
			}
			
			if (StringUtils.isNotBlank(hospCode)) {
				hqlEnd.append(" and shi.hosp_code = :HPC ");
			}
			
			if (StringUtils.isNotBlank(areaId)) {
				hqlEnd.append(" and shi.area_id like :areaId ");
			}
			
			if (StringUtils.isNotBlank(projId)) {
				hqlEnd.append(" and bp.proj_id = :PID");
			}
			
			hqlCount.delete(0,hqlCount.length()-1);
			hqlCount.append(" select COUNT(DISTINCT shi.hosp_id) ");
		}
		
		Query query = this.getSession().createSQLQuery(hql.append(hqlEnd).toString());
		Query queryCount = this.getSession().createSQLQuery(hqlCount.append(hqlEnd).toString());
		
		if (StringUtils.isNotBlank(compId)) {
			query.setParameter("compId", compId);
			queryCount.setParameter("compId", compId);
		}
		if (StringUtils.isNotBlank(hospName)) {
		    query.setParameter("HN",  "%" + hospName + "%");
		    queryCount.setParameter("HN", "%" + hospName + "%");
		}
		
		if (StringUtils.isNotBlank(hospCl)) {
			query.setParameter("HCL", hospCl);
			queryCount.setParameter("HCL", hospCl);
		}
		
		if (StringUtils.isNotBlank(projId)) {
			query.setParameter("PID", projId);
			queryCount.setParameter("PID", projId);
		}

		if (StringUtils.isNotBlank(hospCode)) {
			query.setParameter("HPC", hospCode);
			queryCount.setParameter("HPC", hospCode);
		}
		
		if (StringUtils.isNotBlank(areaId)) {
			query.setParameter("areaId", "%" + areaId + "%");
			queryCount.setParameter("areaId", "%" + areaId + "%");
		}
		BigInteger records = (BigInteger) queryCount.uniqueResult();
		Long count = ((BigInteger) queryCount.list().get(0)).longValue();
		
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		query.setFirstResult((page.getPage() - 1) * page.getMaxResults());
		query.setMaxResults(page.getMaxResults());
		
		List<Map> list = query.list();
		
		list = formatHospInfo(list);
		if (list.size() > 0) {
			page.setRows(list);
		} else {
			page.setRows(0);
		}
		
		page.setRecords(count);
        page.setTotal(new Long(count % page.getMaxResults() == 0 ? count / page.getMaxResults() : count / page.getMaxResults() + 1).intValue());
		return page;
	}
	
	/**
	 * @category 根据条件查询商品目录(中心端）
	 * @author zhouqi
	 * @date 2015年6月15日
	 * @param @param baseDelrelation
	 * @param @return
	 */
	@SuppressWarnings({ "unchecked", "unused", "rawtypes" })
	public Pagination getHospListByPageFromZX(Pagination page) {
		String compType = page.getConditions().get("compType");
		String compId = page.getConditions().get("compId");
		String projId = page.getConditions().get("projId");
		String hospName = page.getConditions().get("hospName");
        String hospCl = page.getConditions().get("hospCl");
        String hospCode = page.getConditions().get("hospCode");
        String areaId = page.getConditions().get("areaId");
		
		StringBuffer hql = new StringBuffer();
		StringBuffer hqlCount = new StringBuffer();
			hql.append(" SELECT DISTINCT A.hosp_id AS hospId,A.hosp_code AS hospCode,A.hosp_name AS hospName,A.hosp_classify AS hospClassify,")
				.append(" A.hosp_level_id AS hospLevelId,A.org_code AS orgCode,B.area_id AS areaId,B.ar_shortname AS areaName,")
				.append(" D.hosp_type AS hosp_type,E.proj_hosp_id")
			    .append(" FROM std_hospinfo A LEFT JOIN dic_area B ON A.area_id = B.area_id LEFT JOIN dic_hosptype D")
			    .append(" ON A.hosp_type_id = D.hosp_type_id JOIN bid_projhospital E ON A.hosp_id = E.hosp_id")
			    .append(" JOIN bid_project F ON E.proj_id = F.proj_id WHERE F.starttime < NOW() AND  F.endtime > NOW()")
				.append(" AND  E.`status` = 1 AND  F.`status` = 1 AND  A.`status` = 1");
			
			if (StringUtils.isNotBlank(hospName)) {
				hql.append(" and A.hosp_name like :HN ");
			}
			
			if (StringUtils.isNotBlank(hospCl)) {
				hql.append(" and A.hosp_classify = :HCL ");
			}
			
			if (StringUtils.isNotBlank(hospCode)) {
				hql.append(" and A.hosp_code = :HPC ");
			}
			
			if (StringUtils.isNotBlank(areaId)) {
				hql.append(" and B.area_id like :areaId ");
			}
			
			if (StringUtils.isNotBlank(projId)) {
				hql.append(" and F.proj_id = :PID");
			}
			hql.append(" group by A.hosp_id");
			hqlCount.append("select count(1) from ("+hql.toString()+") ct"); 
			Query query = this.getSession().createSQLQuery(hql.toString());
			Query queryCount = this.getSession().createSQLQuery(hqlCount.toString());
			
			if (StringUtils.isNotBlank(compId)) {
				query.setParameter("compId", compId);
				queryCount.setParameter("compId", compId);
			}
			if (StringUtils.isNotBlank(hospName)) {
			    query.setParameter("HN",  "%" + hospName + "%");
			    queryCount.setParameter("HN", "%" + hospName + "%");
			}
			
			if (StringUtils.isNotBlank(hospCl)) {
				query.setParameter("HCL", hospCl);
				queryCount.setParameter("HCL", hospCl);
			}
			
			if (StringUtils.isNotBlank(projId)) {
				query.setParameter("PID", projId);
				queryCount.setParameter("PID", projId);
			}

			if (StringUtils.isNotBlank(hospCode)) {
				query.setParameter("HPC", hospCode);
				queryCount.setParameter("HPC", hospCode);
			}
			
			if (StringUtils.isNotBlank(areaId)) {
				query.setParameter("areaId", "%" + areaId + "%");
				queryCount.setParameter("areaId", "%" + areaId + "%");
			}
			
			query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			query.setFirstResult((page.getPage() - 1) * page.getMaxResults());
			query.setMaxResults(page.getMaxResults());
			List<Map> list = query.list();
			long count = ((BigInteger) queryCount.uniqueResult()).longValue();
			
			list = formatHospInfo(list);
			if (list.size() > 0) {
				page.setRows(list);
			} else {
				page.setRows(0);
			}
			page.setRecords(count);
	        page.setTotal(new Long(count % page.getMaxResults() == 0 ? count / page.getMaxResults() : count / page.getMaxResults() + 1).intValue());
			return page;
	}
	
	/**
	 * @category 将医疗机构类型类型由编码变为文字
	 * @author 韩守松
	 * @date   2015年7月8日
	 * @param  @param list
	 * @param  @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected List<Map> formatHospInfo(List<Map> list) {
		
		for(int i=0;i<list.size();i++){
			Map map =	list.get(i);
			String hospClassifyName=HospClassify.getName(Integer.valueOf(map.get("hospClassify")+""));
			String hospLevelName=HospLevel.getName(map.get("hospLevelId")+"");
			list.get(i).put("hospClassifyName", hospClassifyName);
			list.get(i).put("hospLevelName", hospLevelName);
		}
		return list;
	}
	
	/**
	 * @category 中心查看医疗机构目录
	 * @date 2015年11月12日
	 * @param page
	 * @return
	 */
	public Pagination getHospListByZX(Pagination page) {
		StringBuilder sb = new StringBuilder();
		String classifyCode = page.getConditions().get("classifyCode");
		String hospType = page.getConditions().get("hospType");
		sb.append(" SELECT a.hosp_id hospId, a.hosp_code hospCode, a.hosp_name hospName, a.hosp_spel_code, ");
		sb.append(" a.area_id areaId, c.area_name areaName, a.hosp_classify classifyCode,  ");
		sb.append(" f.dic_name classifyName, a.hosp_type_id hospTypeId, d.hosp_type hospTypeName,  ");
		sb.append(" a.hosp_level_id hospLevelId, e.dic_name hospLevelName, a.org_code orgCode  ");
		sb.append(" FROM std_hospinfo a JOIN ( ");
		
		sb.append(" SELECT DISTINCT hosp_code FROM bid_projhospital WHERE proj_status = 1  ");
		sb.append(" AND `status` = 1 AND proj_starttime <= SYSDATE() AND proj_endtime >= SYSDATE() ");
		
		sb.append(" ) b ON a.hosp_code = b.hosp_code  ");
		sb.append(" LEFT JOIN dic_area c ON a.area_id = c.area_id  ");
		sb.append(" LEFT JOIN dic_hosptype d ON a.hosp_type_id = d.hosp_type_id  ");
		sb.append(" LEFT JOIN dic_sysdic e ON e.classify_code = '" + Constants.HOSP_LEVEL + "' AND a.hosp_level_id = e.dic_code  ");
		sb.append(" LEFT JOIN dic_sysdic f ON f.classify_code = '" + Constants.HOSP_CAT + "' AND a.hosp_classify = f.dic_code  ");
		sb.append(" WHERE a.`status` = 1 ");
		
		HospForm hospForm = (HospForm) page.getForm();
		Map<String, Object> params = new HashMap<String, Object>();
		
		if (hospForm != null) {
			if (StringUtils.isNotBlank(hospForm.getHospName())) {
				sb.append(" AND (a.hosp_name LIKE :hospName OR a.hosp_spel_code LIKE :hospName) ");
				params.put("hospName", "%" + hospForm.getHospName() + "%");
			}
			if (StringUtils.isNotBlank(hospForm.getHospCode())) {
				sb.append(" AND a.hosp_code = :hospCode ");
				params.put("hospCode", hospForm.getHospCode());
			}
			if (hospForm.getHospClassify() != null) {
				sb.append(" AND a.hosp_classify = :hospClassify ");
				params.put("hospClassify", hospForm.getHospClassify());
			}
			if (classifyCode != null && classifyCode !="") {
				sb.append(" AND a.hosp_classify = :classifyCode ");
				params.put("classifyCode", classifyCode);
			}
			if (hospType != null && hospType !="") {
				sb.append(" AND a.hosp_level_id = :hospType ");
				params.put("hospType", hospType);
			}
			if (StringUtils.isNotBlank(hospForm.getAreaId())) {
				sb.append(" AND a.area_id LIKE :areaId ");
				params.put("areaId", AreaUtil.escapeAreaId(hospForm.getAreaId()));
			}
		}
		
		dao.getPageMapList(sb.toString(), params, page);
		
		return page;
	}
	
	/**
	 * @category 卫生局查看医疗机构目录
	 * @date 2015年11月12日
	 * @param page
	 * @return
	 */
	public Pagination getHospListByWSJ(Pagination page, SysUser loginUser) {
		StringBuilder sb = new StringBuilder();
		String hospClassifyIds = page.getConditions().get("hospClassifyIds");
		String hospType = page.getConditions().get("hospType");
		sb.append(" SELECT a.area_id FROM std_healthbureau a WHERE a.hea_bur_id = :orgId ");
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sb.toString());
		query.setString("orgId", loginUser.getOrgId());
		String areaId = (String) query.uniqueResult();
		
		if (StringUtils.isBlank(areaId)) {
			return page;
		}
		
		sb = new StringBuilder();
		sb.append(" SELECT a.hosp_id hospId, a.hosp_code hospCode, a.hosp_name hospName,a.hosp_spel_code,  ");
		sb.append(" a.area_id areaId, c.area_name areaName, a.hosp_classify classifyCode,  ");
		sb.append(" f.dic_name classifyName, a.hosp_type_id hospTypeId, d.hosp_type hospTypeName,  ");
		sb.append(" a.hosp_level_id hospLevelId, e.dic_name hospLevelName, a.org_code orgCode  ");
		sb.append(" FROM std_hospinfo a JOIN ( ");
		
		sb.append(" SELECT DISTINCT hosp_code FROM bid_projhospital WHERE proj_status = 1  ");
		sb.append(" AND `status` = 1 AND proj_starttime <= SYSDATE() AND proj_endtime >= SYSDATE() ");
		
		sb.append(" ) b ON a.hosp_code = b.hosp_code  ");
		sb.append(" LEFT JOIN dic_area c ON a.area_id = c.area_id  ");
		sb.append(" LEFT JOIN dic_hosptype d ON a.hosp_type_id = d.hosp_type_id  ");
		sb.append(" LEFT JOIN dic_sysdic e ON e.classify_code = '" + Constants.HOSP_LEVEL + "' AND a.hosp_level_id = e.dic_code  ");
		sb.append(" LEFT JOIN dic_sysdic f ON f.classify_code = '" + Constants.HOSP_CAT + "' AND a.hosp_classify = f.dic_code  ");
		sb.append(" WHERE a.`status` = 1 ");
		sb.append(" AND ").append(AreaUtil.getHospByArea(areaId, "a.area_id"));
		
		HospForm hospForm = (HospForm) page.getForm();
		Map<String, Object> params = new HashMap<String, Object>();
		
		if (hospForm != null) {
			if (StringUtils.isNotBlank(hospForm.getHospName())) {
				sb.append(" AND (a.hosp_name LIKE :hospName OR a.hosp_spel_code LIKE :hospName) ");
				params.put("hospName", "%" + hospForm.getHospName() + "%");
			}
			if (StringUtils.isNotBlank(hospForm.getHospCode())) {
				sb.append(" AND a.hosp_code = :hospCode ");
				params.put("hospCode", hospForm.getHospCode());
			}
			if (hospForm.getHospClassify() != null) {
				sb.append(" AND a.hosp_classify = :hospClassify ");
				params.put("hospClassify", hospForm.getHospClassify());
			}
			if (StringUtils.isNotBlank(hospClassifyIds)) {
				sb.append(" AND a.hosp_classify = :hospClassifyIds ");
				params.put("hospClassifyIds", hospClassifyIds);
			}
			if (hospType != null && hospType !="") {
				sb.append(" AND a.hosp_level_id = :hospType ");
				params.put("hospType", hospType);
			}
			if (StringUtils.isNotBlank(hospForm.getAreaId())) {
				sb.append(" AND a.area_id LIKE :areaId ");
				params.put("areaId", AreaUtil.escapeAreaId(hospForm.getAreaId()));
			}
		}
		
		dao.getPageMapList(sb.toString(), params, page);
		
		return page;
	}
	
	/**
	 * @category 医疗机构查看医疗机构目录
	 * @date 2015年11月12日
	 * @param page
	 * @return
	 */
	public Pagination getHospListByYY(Pagination page) {
		StringBuilder sb = new StringBuilder();
		sb.append(" SELECT a.hosp_id hospId, a.hosp_code hospCode, a.hosp_name hospName,a.hosp_spel_code,  ");
		sb.append(" a.area_id areaId, c.area_name areaName, a.hosp_classify classifyCode,  ");
		sb.append(" f.dic_name classifyName, a.hosp_type_id hospTypeId, d.hosp_type hospTypeName,  ");
		sb.append(" a.hosp_level_id hospLevelId, e.dic_name hospLevelName, a.org_code orgCode  ");
		sb.append(" FROM std_hospinfo a JOIN ( ");
		
		sb.append(" SELECT DISTINCT a.hosp_code FROM bid_projhospital a JOIN ( ");
		sb.append(" SELECT proj_id FROM bid_projhospital WHERE proj_status = 1  ");
		sb.append(" AND `status` = 1 AND proj_starttime <= SYSDATE() AND proj_endtime >= SYSDATE() ");
		sb.append(" AND hosp_code = :hospCode ) b ON a.proj_id = b.proj_id  ");
		
		sb.append(" ) b ON a.hosp_code = b.hosp_code  ");
		sb.append(" LEFT JOIN dic_area c ON a.area_id = c.area_id  ");
		sb.append(" LEFT JOIN dic_hosptype d ON a.hosp_type_id = d.hosp_type_id  ");
		sb.append(" LEFT JOIN dic_sysdic e ON e.classify_code = '" + Constants.HOSP_LEVEL + "' AND a.hosp_level_id = e.dic_code  ");
		sb.append(" LEFT JOIN dic_sysdic f ON f.classify_code = '" + Constants.HOSP_CAT + "' AND a.hosp_classify = f.dic_code  ");
		sb.append(" WHERE a.`status` = 1 ");
		
		HospForm hospForm = (HospForm) page.getForm();
		Map<String, Object> params = new HashMap<String, Object>();
		
		if (hospForm != null) {
			params.put("hospCode", hospForm.getHospCode());
			
			if (StringUtils.isNotBlank(hospForm.getHospName())) {
				sb.append(" AND (a.hosp_name LIKE :hospName OR a.hosp_spel_code LIKE :hospName) ");
				params.put("hospName", "%" + hospForm.getHospName() + "%");
			}
			if (StringUtils.isNotBlank(hospForm.getHospCode())) {
				sb.append(" AND a.hosp_code = :hospCode ");
				params.put("hospCode", hospForm.getHospCode());
			}
			if (hospForm.getHospClassify() != null) {
				sb.append(" AND a.hosp_classify = :hospClassify ");
				params.put("hospClassify", hospForm.getHospClassify());
			}
			if (StringUtils.isNotBlank(hospForm.getAreaId())) {
				sb.append(" AND a.area_id LIKE :areaId ");
				params.put("areaId", AreaUtil.escapeAreaId(hospForm.getAreaId()));
			}
		}
		
		dao.getPageMapList(sb.toString(), params, page);
		
		return page;
	}
	
	/**
	 * @category 生产企业查看医疗机构目录
	 * @date 2015年11月12日
	 * @param page
	 * @return
	 */
	public Pagination getHospListBySC(Pagination page) {
		StringBuilder sb = new StringBuilder();
		sb.append(" SELECT a.hosp_id hospId, a.hosp_code hospCode, a.hosp_name hospName,a.hosp_spel_code,  ");
		sb.append(" a.area_id areaId, c.area_name areaName, a.hosp_classify classifyCode,  ");
		sb.append(" f.dic_name classifyName, a.hosp_type_id hospTypeId, d.hosp_type hospTypeName,  ");
		sb.append(" a.hosp_level_id hospLevelId, e.dic_name hospLevelName, a.org_code orgCode  ");
		sb.append(" FROM std_hospinfo a JOIN ( ");
		
		sb.append(" SELECT a.hosp_code FROM bid_projhospital a JOIN ( ");
		sb.append(" SELECT DISTINCT proj_id FROM bid_projdrugs ");
		sb.append(" WHERE proj_status = 1 AND `status` = 1 AND comp_code = :compCode ");
		sb.append(" AND proj_starttime <= SYSDATE() AND proj_endtime >= SYSDATE() ");
		sb.append(" ) b ON a.proj_id = b.proj_id  ");
		
		sb.append(" ) b ON a.hosp_code = b.hosp_code  ");
		sb.append(" LEFT JOIN dic_area c ON a.area_id = c.area_id  ");
		sb.append(" LEFT JOIN dic_hosptype d ON a.hosp_type_id = d.hosp_type_id  ");
		sb.append(" LEFT JOIN dic_sysdic e ON e.classify_code = '" + Constants.HOSP_LEVEL + "' AND a.hosp_level_id = e.dic_code  ");
		sb.append(" LEFT JOIN dic_sysdic f ON f.classify_code = '" + Constants.HOSP_CAT + "' AND a.hosp_classify = f.dic_code  ");
		sb.append(" WHERE a.`status` = 1 ");
		
		HospForm hospForm = (HospForm) page.getForm();
		Map<String, Object> params = new HashMap<String, Object>();
		
		if (hospForm != null) {
			params.put("compCode", hospForm.getCompCode());
			
			if (StringUtils.isNotBlank(hospForm.getHospName())) {
				sb.append(" AND (a.hosp_name LIKE :hospName OR a.hosp_spel_code LIKE :hospName) ");
				params.put("hospName", "%" + hospForm.getHospName() + "%");
			}
			if (StringUtils.isNotBlank(hospForm.getHospCode())) {
				sb.append(" AND a.hosp_code = :hospCode ");
				params.put("hospCode", hospForm.getHospCode());
			}
			if (hospForm.getHospClassify() != null) {
				sb.append(" AND a.hosp_classify = :hospClassify ");
				params.put("hospClassify", hospForm.getHospClassify());
			}
			if (StringUtils.isNotBlank(hospForm.getAreaId())) {
				sb.append(" AND a.area_id LIKE :areaId ");
				params.put("areaId", AreaUtil.escapeAreaId(hospForm.getAreaId()));
			}
		}
		
		dao.getPageMapList(sb.toString(), params, page);
		
		return page;
	}
	
	/**
	 * @category 配送企业查看医疗机构目录
	 * @date 2015年11月12日
	 * @param page
	 * @return
	 */
	public Pagination getHospListByPS(Pagination page) {
		StringBuilder sb = new StringBuilder();
		sb.append(" SELECT a.hosp_id hospId, a.hosp_code hospCode, a.hosp_name hospName,a.hosp_spel_code  ");
		sb.append(" a.area_id areaId, c.area_name areaName, a.hosp_classify classifyCode,  ");
		sb.append(" f.dic_name classifyName, a.hosp_type_id hospTypeId, d.hosp_type hospTypeName,  ");
		sb.append(" a.hosp_level_id hospLevelId, e.dic_name hospLevelName, a.org_code orgCode  ");
		sb.append(" FROM std_hospinfo a JOIN ( ");
		
		sb.append(" SELECT a.hosp_code FROM bid_projhospital a JOIN ( ");
		sb.append(" SELECT DISTINCT proj_id FROM bid_projcompany ");
		sb.append(" WHERE proj_status = 1 AND `status` = 1 AND comp_code = :compCode ");
		sb.append(" AND proj_starttime <= SYSDATE() AND proj_endtime >= SYSDATE() ");
		sb.append(" ) b ON a.proj_id = b.proj_id  ");
		
		sb.append(" ) b ON a.hosp_code = b.hosp_code  ");
		sb.append(" LEFT JOIN dic_area c ON a.area_id = c.area_id  ");
		sb.append(" LEFT JOIN dic_hosptype d ON a.hosp_type_id = d.hosp_type_id  ");
		sb.append(" LEFT JOIN dic_sysdic e ON e.classify_code = '" + Constants.HOSP_LEVEL + "' AND a.hosp_level_id = e.dic_code  ");
		sb.append(" LEFT JOIN dic_sysdic f ON f.classify_code = '" + Constants.HOSP_CAT + "' AND a.hosp_classify = f.dic_code  ");
		sb.append(" WHERE a.`status` = 1 ");
		
		HospForm hospForm = (HospForm) page.getForm();
		Map<String, Object> params = new HashMap<String, Object>();
		
		if (hospForm != null) {
			params.put("compCode", hospForm.getCompCode());
			
			if (StringUtils.isNotBlank(hospForm.getHospName())) {
				sb.append(" AND (a.hosp_name LIKE :hospName OR a.hosp_spel_code LIKE :hospName) ");
				params.put("hospName", "%" + hospForm.getHospName() + "%");
			}
			if (StringUtils.isNotBlank(hospForm.getHospCode())) {
				sb.append(" AND a.hosp_code = :hospCode ");
				params.put("hospCode", hospForm.getHospCode());
			}
			if (hospForm.getHospClassify() != null) {
				sb.append(" AND a.hosp_classify = :hospClassify ");
				params.put("hospClassify", hospForm.getHospClassify());
			}
			if (StringUtils.isNotBlank(hospForm.getAreaId())) {
				sb.append(" AND a.area_id LIKE :areaId ");
				params.put("areaId", AreaUtil.escapeAreaId(hospForm.getAreaId()));
			}
		}
		
		dao.getPageMapList(sb.toString(), params, page);
		
		return page;
	}

}
